#include "StdAfx.h"
#include "CategoryManager.h"
#include "..\Knowledge\KnowlTypeTree.h"


CCategoryManager::CCategoryManager(void)
{
}


CCategoryManager::~CCategoryManager(void)
{
}
bool CCategoryManager::Add(CKnowlTypeTree* pViewTree,Category* pCate)
{
	//CString strSQL = _T("inset into km_category(parent,category,classtype)values(?,?,?)");
	static const LPCWSTR strSQL = L"insert into km_category(parent,category,classtype,Icon,PwMasterId)values(?,?,?,?,?);";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(strSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,pCate->m_Parent);
	pSQLStatement->Bind(2,pCate->m_strCate);
	pSQLStatement->Bind(3,pCate->m_strClass);
	pSQLStatement->Bind(4,pCate->m_Icon);
	pSQLStatement->Bind(5,pCate->m_MasterId);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());

	if(Res)
	{
		m_pDB->CommitTransaction();
		
		//HTREEITEM hRoot = pViewTree->GetSelectedItem();
		HTREEITEM hItem = pViewTree->GetSelectedItem();
		if(!hItem)
			return false;
		
		
		HTREEITEM hItem1 = pViewTree->InsertItem(pCate->m_strCate,pCate->m_Icon,49,hItem,NULL);
		//pSQLStatement->Reset();
		CComPtr<ISQLiteComStatement3> pSQLStatement1;
		m_pDB->PrepareStatement(L"select max(classid) from km_category where parent=?;",&pSQLStatement1);
		pSQLStatement1->Bind(1,pCate->m_Parent);
		CComPtr<Skylark::ISQLiteComResultSet3> pRs;
		HRESULT hr = pSQLStatement1->ExecuteQuery(&pRs);
		if(!SUCCEEDED(hr) || pRs->IsEof())
			return FALSE;
		int classid = pRs->GetInt(0);
		pViewTree->SetItemData(hItem1,(DWORD_PTR)classid);
		
		return true;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return false;
	}
}
bool CCategoryManager::Add(Category* pCate)
{
	//CString strSQL = _T("inset into km_category(parent,category,classtype)values(?,?,?)");
	static const LPCWSTR strSQL = L"insert into km_category(parent,category,classtype,Icon,PwMasterId)values(?,?,?,?,?);";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(strSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,pCate->m_Parent);
	pSQLStatement->Bind(2,pCate->m_strCate);
	pSQLStatement->Bind(3,pCate->m_strClass);
	pSQLStatement->Bind(4,pCate->m_Icon);
	pSQLStatement->Bind(5,pCate->m_MasterId);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());

	if(Res)
	{
		m_pDB->CommitTransaction();
		CComPtr<ISQLiteComStatement3> pSQLStatement1;
		m_pDB->PrepareStatement(L"select max(classid) from km_category where classtype=?;",&pSQLStatement1);
		pSQLStatement1->Bind(1,pCate->m_strClass);
		CComPtr<Skylark::ISQLiteComResultSet3> pRs;
		HRESULT hr = pSQLStatement1->ExecuteQuery(&pRs);
		if(!SUCCEEDED(hr) || pRs->IsEof())
			return FALSE;
		pCate->m_ID  = pRs->GetInt(0);
		return true;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return false;
	}
}
/*
int CCategoryManager::Add(int parent,CString strCategory,const char* strClassType)
{
	//CString strSQL = _T("inset into km_category(parent,category,classtype)values(?,?,?)");
	const char* strSQL = "insert into km_category(parent,category,classtype)values(?,?,?);";
	CUnicodeToMultiByte mb(strCategory);
	m_db.TransactionBegin();
	m_db.Compile(strSQL);
	m_db.Bind(1,parent);
	m_db.Bind(2,mb);
	m_db.Bind(3,strClassType);
	int ret = m_db.ExecDML();

	if(ret > 0)
	{
		m_db.TransactionCommit();
		strSQL = "select max(classid) from km_category where parent=?;";
		m_db.Compile(strSQL);
		m_db.Bind(1,parent);
		CSQLQuery query = m_db.Execute();
		if(!query.Eof())
		{
			int classid = query.GetIntField(0);
			return classid;
		}
		return 0;
	}
	else
	{
		m_db.TransactionRollback();
		return -1;
	}
}
*/

bool CCategoryManager::Load(CKnowlTypeTree* pViewTree,LPCTSTR strClassType,int nMasterId )
{
	if(!pViewTree)
		return false;
	//LPCTSTR
	HTREEITEM hRoot;
	if(_tcscmp(strClassType,L"ART") == 0)
		hRoot= pViewTree->InsertItem(BkString::Get(3), 60, 49,NULL,NULL);
	else if(_tcscmp(strClassType,L"DICT") == 0)
		hRoot= pViewTree->InsertItem(BkString::Get(4), 57, 49,NULL,NULL);
	else
		hRoot= pViewTree->InsertItem(BkString::Get(61), 52, 49,NULL,NULL);
	pViewTree->SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);
	pViewTree->SetItemData(hRoot,(DWORD_PTR)0);
	pViewTree->Expand(hRoot, TVE_EXPAND);
	//CString strSQL = _T("select classid,parent,category from km_category where classtype=? order by parent desc ");
	//CString strSQL = _T("select classid,parent,category from km_category  order by parent desc ;");
	static LPCWSTR szSql = L"select classid,parent,category,icon from km_category where classtype=? order by parent ;";
	if(nMasterId > 0)
		szSql = L"select classid,parent,category,icon from km_category where classtype=? and pwmasterid=? order by parent ;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSql,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,strClassType);
	if(nMasterId > 0)
		pSQLStatement->Bind(2,nMasterId);
	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	if(!SUCCEEDED(hr) || pRs->IsEof())
		return FALSE;
	while(!pRs->IsEof())
	{
		int classId = pRs->GetInt(0);
		int parent = pRs->GetInt(1);
		int icon = pRs->GetInt(3);
		HTREEITEM hClass;
		HTREEITEM hItem = pViewTree->FindItem(hRoot,parent);
		CString strCate = pRs->GetAsString(2);
		if(hItem)
			hClass = pViewTree->InsertItem(strCate, icon, 49, hItem,NULL);
		else
			hClass = pViewTree->InsertItem(strCate, icon, 49, hRoot,NULL);
		pViewTree->SetItemData(hClass,(DWORD_PTR)classId);
		pRs->NextRow();
		pViewTree->Expand(hClass, TVE_EXPAND);
	}
	return true;
}
bool CCategoryManager::Remove(CKnowlTypeTree* pViewTree,int iClassID)
{
	if(!pViewTree)
		return false;
	HTREEITEM hItem= pViewTree->GetSelectedItem();
	if(Remove(iClassID))
	{
		pViewTree->DeleteItem(hItem);
		return true;
	}
	else
		return false;
}
bool CCategoryManager::Remove(int iClassID)
{
	
	static LPCWSTR szSql = L"delete from km_category where classid=?;delete from km_category where parent=?;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(szSql,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iClassID);
//	pSQLStatement->Bind(2,iClassID);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());
	
	if(Res)
	{
		m_pDB->CommitTransaction();
		return true;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return false;
	}
}
bool CCategoryManager::Rename(CKnowlTypeTree* pViewTree,int iClassID,CString strCategory)
{
	if(!pViewTree)
		return false;
	HTREEITEM hItem= pViewTree->GetSelectedItem();
	if(Rename(iClassID,strCategory))
	{
		pViewTree->SetItemText(hItem,strCategory);
		return true;
	}
	else
		return false;
}
bool CCategoryManager::Rename(int iClassID,CString strCategory)
{
	static LPCWSTR szSql = L"update km_category set category=? where classid=?;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(szSql,&pSQLStatement);
	pSQLStatement->Bind(1,strCategory);
	pSQLStatement->Bind(2,iClassID);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());
	if(Res > 0)
	{
		m_pDB->CommitTransaction();
		return true;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return false;
	}
}
bool CCategoryManager::ChangeParent(int iParent,int iClassID)
{
	static LPCWSTR szSql = L"update km_category set parent=? where classid=?;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(szSql,&pSQLStatement);
	pSQLStatement->Bind(1,iParent);
	pSQLStatement->Bind(2,iClassID);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());
	if(Res > 0)
	{
		m_pDB->CommitTransaction();
		return true;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return false;
	}
}
CategoryList* CCategoryManager::GetCategoryList(const CString& strClassType,int nMasterId )
{
	static LPCWSTR szSql = L"select classid,parent,category,icon from km_category where classtype=? order by classid ;";
	if(nMasterId > 0)
		szSql = L"select classid,parent,category,icon from km_category where classtype=? and pwmasterid=? order by parent ;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSql,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,strClassType);
	if(nMasterId > 0)
		pSQLStatement->Bind(2,nMasterId);
	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	
	
	if(!SUCCEEDED(hr) || pRs->IsEof())
		return NULL;
	CategoryList* list = new CategoryList();
	while(!pRs->IsEof())
	{
		Category* cate = new Category;
		cate->m_ID = pRs->GetInt(0);
		cate->m_Parent  = pRs->GetInt(1);
		cate->m_Icon = pRs->GetInt(3);
		cate->m_MasterId = nMasterId;
		cate->m_strCate = pRs->GetAsString(2);
		cate->m_strClass = strClassType;
		list->AddTail(cate);
		pRs->NextRow();
	}
	return list;
}